package com.java110.mall.shop.bmo.shopAudit.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.user.UserDto;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.shop.bmo.shopAudit.IUpdateShopAuditBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.java110.intf.shop.IShopAuditInnerServiceSMO;
import com.java110.utils.util.Assert;
import com.java110.po.shop.ShopAuditPo;

import java.util.List;

@Service("updateShopAuditBMOImpl")
public class UpdateShopAuditBMOImpl implements IUpdateShopAuditBMO {

    @Autowired
    private IShopAuditInnerServiceSMO shopAuditInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;


    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    /**
     * @param shopAuditPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(ShopAuditPo shopAuditPo) {

        int flag = shopAuditInnerServiceSMOImpl.updateShopAudit(shopAuditPo);

        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }


    @Override
    @Java110Transactional
    public ResponseEntity<String> auditShop(JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "storeId", "未包含商户");
        Assert.hasKeyAndValue(reqJson, "auditId", "未包含审核信息");
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺");
        Assert.hasKeyAndValue(reqJson, "userId", "未包含审核人");
        Assert.hasKeyAndValue(reqJson, "state", "未包含审核状态");
        Assert.hasKeyAndValue(reqJson, "auditOpinion", "未包含审核意见");

        UserDto userDto = new UserDto();
        userDto.setUserId(reqJson.getString("userId"));
        List<UserDto> userDtos = userInnerServiceSMOImpl.queryUsers(userDto);

        Assert.listOnlyOne(userDtos, "审核人不存在");
        //插入审核表
//        WorkflowAuditMessagePo workflowAuditMessagePo = new WorkflowAuditMessagePo();
//        workflowAuditMessagePo.setAuditId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_auditId));
//        workflowAuditMessagePo.setAuditLink(userDtos.get(0).getTel());
//        workflowAuditMessagePo.setAuditPerson(userDtos.get(0).getName());
//        workflowAuditMessagePo.setShareType(WorkflowAuditMessageDto.SHARE_TYPE_STORE);
//        workflowAuditMessagePo.setShareId(reqJson.getString("storeId"));
//        workflowAuditMessagePo.setAuditPersonId(userDtos.get(0).getUserId());
//        workflowAuditMessagePo.setBusinessType(WorkflowAuditMessageDto.BUSINESS_TYPE_SHOP);
//        workflowAuditMessagePo.setMessage(reqJson.getString("auditOpinion"));
//        workflowAuditMessagePo.setPreAuditId("-1");
//        workflowAuditMessagePo.setBusinessKey(reqJson.getString("shopId"));
//        workflowAuditMessagePo.setState(reqJson.getString("state"));
//        int flag = 0;//workflowAuditMessageInnerServiceSMOImpl.saveWorkflowAuditMessage(workflowAuditMessagePo);
//
//        if (flag < 1) {
//            return ResultVo.error("审核失败");
//        }
//        ShopAuditDto shopAuditDto = new ShopAuditDto();
//        shopAuditDto.setAuditId(reqJson.getString("auditId"));
//        List<ShopAuditDto> shopAuditDtos = shopAuditInnerServiceSMOImpl.queryShopAudits(shopAuditDto);
//        if (ListUtil.isNull(shopAuditDtos)) {
//            return ResultVo.error("审核失败，未查询到审核信息。");
//        }
//        ShopAuditPo shopAuditPo = new ShopAuditPo();
//        shopAuditPo.setStoreId(reqJson.getString("storeId"));
//        shopAuditPo.setShopId(reqJson.getString("shopId"));
//        shopAuditPo.setAuditId(reqJson.getString("auditId"));
//        shopAuditPo.setAuditOpinion(reqJson.getString("auditOpinion"));
//        if (WorkflowAuditMessageDto.STATE_Y.equals(reqJson.getString("state"))) {//审核通过
//            shopAuditPo.setState(ShopDto.STATE_Y);
//            ShopPo shopPo = new ShopPo();
//            shopPo.setShopLogo(shopAuditDtos.get(0).getShopLogo());
//            shopPo.setAreaCode(shopAuditDtos.get(0).getAreaCode());
//            shopPo.setOpenType(shopAuditDtos.get(0).getOpenType());
//            shopPo.setReturnAddress(shopAuditDtos.get(0).getReturnAddress());
//            shopPo.setReturnLink(shopAuditDtos.get(0).getReturnLink());
//            shopPo.setReturnPerson(shopAuditDtos.get(0).getReturnPerson());
//            shopPo.setShopDesc(shopAuditDtos.get(0).getShopDesc());
//            shopPo.setShopId(shopAuditDtos.get(0).getShopId());
//            shopPo.setShopType(shopAuditDtos.get(0).getShopType());
//            shopPo.setShopName(shopAuditDtos.get(0).getShopName());
//            shopInnerServiceSMOImpl.updateShop(shopPo);
//        } else if (WorkflowAuditMessageDto.STATE_N.equals(reqJson.getString("state"))) {
//            shopAuditPo.setState(ShopDto.STATE_N);
//        } else {
//            shopAuditPo.setState(ShopDto.STATE_APPLY);
//        }
//        shopAuditInnerServiceSMOImpl.updateShopAudit(shopAuditPo);
        return ResultVo.success();
    }


}
